﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MsDAL;
using Model;
using Components;

namespace UIMarker
{
    public partial class FrmPopSourceSlt : Form
    {
        string insertTmp = @"INSERT INTO Sys8a00(popno,ptable,pkey,fields,pagesize,orderfid,strwhere,keyref,reffield,issavekey,description,width,height) VALUES
('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}','{11}','{12}');";
        string updateTmp = @"UPDATE Sys8a00 SET [ptable] = '{1}',
[pkey] = '{2}',
[fields] = '{3}',
[pagesize] = '{4}',
[orderfid] = '{5}',
[strwhere] = '{6}',
[keyref] = '{7}',
[reffield] = '{8}',
[issavekey] = '{9}',
[description] = '{10}',
[width] = '{11}',
[height] = '{12}'
WHERE [popno] = '{0}'; ";

        Sys8a00 model = null;

        Sys8a00DAL dal = new Sys8a00DAL();
        CommonDAL comdal = new CommonDAL();
        public FrmPopSourceSlt()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            DataTable dt = dal.GetList(null, null);
            this.dgvMain.DataSource = dt;
        }

        private void dgvMain_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.dgvMain.SelectedRows.Count == 0)
            {
                MessageBox.Show("必须选中行!");
                return;
            }

            DataGridViewRow row = this.dgvMain.SelectedRows[0];

            model = new Sys8a00();
            UIHelper.FillModelFromDataGridViewRow(model, row);

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        public Sys8a00 GetSelectedData()
        {
            return model;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            dgvMain_MouseDoubleClick(null, null);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void dgvMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) //回车选定数据
            {
                dgvMain_MouseDoubleClick(null, null);
            }
        }

        private void tspDel_Click(object sender, EventArgs e)
        {
            if (this.dgvMain.SelectedRows.Count == 0)
            {
                MessageBox.Show("必须选中行!");
                return;
            }

            DataGridViewRow row = this.dgvMain.SelectedRows[0];

            Sys8a00 dmodel = new Sys8a00();
            dmodel.Popno = Validation.ToString(row.Cells["popno"].Value);

            List<SqlApp> lst = new List<SqlApp>();
            lst.Add(dal.Delete(dmodel));

            string errmsg = comdal.SaveData(lst);
            if (string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show("删除OK!", "提示");
            }
            else
            {
                MessageBox.Show(errmsg, "提示");
            }

            this.dgvMain.DataSource = null;
            Init();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (this.dgvMain.SelectedRows.Count == 0)
            {
                MessageBox.Show("必须选中行!");
                return;
            }

            ToolStripButton s = sender as ToolStripButton;

            DataGridViewRow row = this.dgvMain.SelectedRows[0];

            model = new Sys8a00();
            UIHelper.FillModelFromDataGridViewRow(model, row);

            string temp = s == toolStripButton3 ? insertTmp : updateTmp;

            string cnt = string.Format(temp,
                model.Popno, model.Ptable.Replace("'", "''"), model.Pkey, model.Fields.Replace("'", "''"), model.Pagesize, model.Orderfid, model.Strwhere,
                model.Keyref, model.Reffield, model.Issavekey, model.Description, model.Width, model.Height);

            //将内容复制到剪切板中
            Clipboard.SetDataObject(cnt);

            model = null;
        }
    }
}
